package com.Renting.service.impl;

import com.Renting.dao.GainDao;
import com.Renting.dao.RentingDao;
import com.Renting.domain.Renting;
import com.Renting.domain.RentingUser;
import com.Renting.domain.record;
import com.Renting.service.ServiceRenting;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.List;

@Service
public class ServiceRentingImpl implements ServiceRenting {

    @Resource
    RentingDao rentingDao;
    @Resource
    GainDao gainDao;

    //预约程序
    @Override
    public boolean ServiceRentingSubscription(Renting renting, HttpSession session) {
        //获取该房间的目前状态是否为未出租
       Renting ren= rentingDao.RentingStateDao(renting);
       //获取当前用户的数据信息
       RentingUser user=(RentingUser) session.getAttribute("Message");
       //通过该房间的信息获取租用者的信息
       RentingUser ReturnUser= gainDao.GetUserRank(ren);
       //判断该房间是否已经被占用,如果被占用,则判断当前用户等级是否大于租用者
       if(ren.getState()<1||user.getRank()>ReturnUser.getRank()){
           //如果该房间有租用者且被抢房,则将房间被抢的消息发送给租用者
           if(ren.getRoomUser()!=null){
               ReturnUser.setPalter(1);
               rentingDao.RentingHint(ReturnUser);
           }
           //预约成功后修改房间信息
            renting.setRoomUser(user.getPname());
            rentingDao.RentingSubscribeDao(renting);
            rentingDao.RentingSubscribeSetDao(user.getUsername(),user.getPname(),renting.getNumber());
            return true;
        }else{
            return false;
        }

    }


    //支付后记录该支付数据
    @Override
    public boolean ServiceRentingPay(Renting renting, HttpSession session) {
       int pay= rentingDao.RentingPay(renting);
       record re= new record();
       RentingUser user= (RentingUser) session.getAttribute("Message");
       LocalDateTime dateTime = LocalDateTime.now();
       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
       Renting rent= gainDao.GetPrice(renting);
       re.setMoney(rent.getPrice());
       re.setRname(user.getPname());
       re.setRtime(dateTime.format(formatter));
       re.setRnameid(user.getPid());
       re.setRoomid(renting.getNumber());
       rentingDao.RentingCount(re);
        List<Renting> ListRenting= gainDao.getRuntingDao();
        session.setAttribute("ListRenting",ListRenting);
        if(pay!=0){
            return true;
        }else{
            return false;
        }

    }

    //取消入住
    @Override
    public boolean ServiceRentingPayCancel(Renting renting) {
        int result= rentingDao.RentingPayCancel(renting);
        if(result!=0){
            return true;
        }else{
            return false;
        }
    }

    //支付入住
    @Override
    public boolean ServiceRentingRoom(Renting renting) {
       Renting ren= rentingDao.RentingRoomDao(renting);
        if(ren.getState()==2){
            return true;
        }else{
            return false;
        }

    }
}
